/**
 * 研发项目数据模型
 */

const MultiModel = require('../../../framework/database/multi_model.js');
const researchConstants = require('./research_constants.js');

class ResearchProjectModel extends MultiModel {

    /**
     * 构造函数
     */
    constructor() {
        super();
        this.setCollection(researchConstants.COLLECTION_NAMES.PROJECT);
    }

    /**
     * 设置集合名称
     * @param {string} collectionName 集合名称
     */
    setCollection(collectionName) {
        this._collection = collectionName;
    }

    /**
     * 获取数据库表结构
     */
    static DB_STRUCTURE() {
        return researchConstants.PROJECT_SCHEMA;
    }

    /**
     * 字段前缀
     * @deprecated 已废弃，使用小驼峰命名不再需要前缀
     */
    static FIELD_PREFIX = 'PROJECT_';

    /**
     * 获取状态名称
     * @param {number} status 状态值
     * @returns {string} 状态名称
     */
    static getStatusName(status) {
        switch (status) {
            case researchConstants.PROJECT_STATUS.PROJECT_REVIEW:
                return '项目审核';
            case researchConstants.PROJECT_STATUS.DORMANT:
                return '休眠';
            case researchConstants.PROJECT_STATUS.ACTIVE:
                return '活动';
            case researchConstants.PROJECT_STATUS.COMPLETED:
                return '已完成';
            default:
                return '未知';
        }
    }

    /**
     * 获取子状态名称
     * @param {number} status 主状态值
     * @param {string} subStatus 子状态值
     * @returns {string} 子状态名称
     */
    static getSubStatusName(status, subStatus) {
        if (!subStatus) return '';
        
        // 项目审核子状态
        if (status === researchConstants.PROJECT_STATUS.PROJECT_REVIEW) {
            const subStatusMap = {
                [researchConstants.PROJECT_REVIEW_SUB_STATUS.DIRECTOR]: '董事长',
                [researchConstants.PROJECT_REVIEW_SUB_STATUS.RD_MANAGER]: '研发经理',
                [researchConstants.PROJECT_REVIEW_SUB_STATUS.ASSIGNMENT]: '分配'
            };
            return subStatusMap[subStatus] || '未知';
        }

        // 休眠子状态
        if (status === researchConstants.PROJECT_STATUS.DORMANT) {
            const subStatusMap = {
                [researchConstants.DORMANT_SUB_STATUS.REVIEW]: '审核',
                [researchConstants.DORMANT_SUB_STATUS.ARCHIVE]: '归档'
            };
            return subStatusMap[subStatus] || '未知';
        }

        return '未知';
    }

    /**
     * 获取活动状态名称
     * @param {string} activeState 活动状态值
     * @returns {string} 活动状态名称
     */
    static getActiveStateName(activeState) {
        if (!activeState) return '';
        
        const activeStateMap = {
            [researchConstants.ACTIVE_STATE.HARDWARE_CONFIRM]: '硬件确认',
            [researchConstants.ACTIVE_STATE.HARDWARE]: '硬件',
            [researchConstants.ACTIVE_STATE.DEBUG]: '调试',
            [researchConstants.ACTIVE_STATE.DATA_SYNC]: '资料同步',
            [researchConstants.ACTIVE_STATE.TESTING]: '测试',
            [researchConstants.ACTIVE_STATE.CUSTOMER_CONFIRM]: '客户确认',
            [researchConstants.ACTIVE_STATE.ORDER_PREPARATION]: '订单准备',
            [researchConstants.ACTIVE_STATE.PRODUCTION]: '生产',
            [researchConstants.ACTIVE_STATE.PRODUCT_FEEDBACK]: '待反馈'
        };

        return activeStateMap[activeState] || '未知';
    }

    /**
     * 获取活动子状态名称
     * @param {string} activeState 活动状态值
     * @param {string} activeSubState 活动子状态值
     * @returns {string} 活动子状态名称
     */
    static getActiveSubStateName(activeState, activeSubState) {
        if (!activeState || !activeSubState) return '';
        
        // 硬件子状态
        if (activeState === researchConstants.ACTIVE_STATE.HARDWARE) {
            const subStateMap = {
                [researchConstants.HARDWARE_SUB_STATE.DESIGN]: '设计',
                [researchConstants.HARDWARE_SUB_STATE.REVIEW]: '审核',
                [researchConstants.HARDWARE_SUB_STATE.SAMPLE]: '打样',
                [researchConstants.HARDWARE_SUB_STATE.PRODUCTION_SAMPLE]: '制样',
                [researchConstants.HARDWARE_SUB_STATE.IN_STOCK]: '在库'
            };
            return subStateMap[activeSubState] || '未知';
        }
        
        // 测试子状态
        if (activeState === researchConstants.ACTIVE_STATE.TESTING) {
            const subStateMap = {
                [researchConstants.TESTING_SUB_STATE.TESTING]: '测试中',
                [researchConstants.TESTING_SUB_STATE.NO_TEST_REVIEW]: '免测审核'
            };
            return subStateMap[activeSubState] || '未知';
        }
        
        // 客户确认子状态
        if (activeState === researchConstants.ACTIVE_STATE.CUSTOMER_CONFIRM) {
            const subStateMap = {
                [researchConstants.CUSTOMER_CONFIRM_SUB_STATE.DELIVERY]: '寄样',
                [researchConstants.CUSTOMER_CONFIRM_SUB_STATE.TESTING]: '测试',
                [researchConstants.CUSTOMER_CONFIRM_SUB_STATE.NO_SAMPLE_REVIEW]: '无签样审核',
                [researchConstants.CUSTOMER_CONFIRM_SUB_STATE.SAMPLE_IN_STOCK]: '样品在库',
                [researchConstants.CUSTOMER_CONFIRM_SUB_STATE.ARCHIVE]: '归档'
            };
            return subStateMap[activeSubState] || '未知';
        }
        
        // 订单准备子状态
        if (activeState === researchConstants.ACTIVE_STATE.ORDER_PREPARATION) {
            const subStateMap = {
                [researchConstants.ORDER_PREPARATION_SUB_STATE.WAITING]: '待量产',
                [researchConstants.ORDER_PREPARATION_SUB_STATE.REVIEW]: '审核',
                [researchConstants.ORDER_PREPARATION_SUB_STATE.DOC_CONTROL]: '文控',
                [researchConstants.ORDER_PREPARATION_SUB_STATE.SUPPLEMENT]: '补充资料',
                [researchConstants.ORDER_PREPARATION_SUB_STATE.PRE_MEETING]: '产前会',
                [researchConstants.ORDER_PREPARATION_SUB_STATE.PREPARATION]: '生产准备'
            };
            return subStateMap[activeSubState] || '未知';
        }
        
        // 生产子状态
        if (activeState === researchConstants.ACTIVE_STATE.PRODUCTION) {
            const subStateMap = {
                [researchConstants.PRODUCTION_SUB_STATE.PRODUCING]: '生产中',
                [researchConstants.PRODUCTION_SUB_STATE.PE_HANDLING]: 'PE处理',
                [researchConstants.PRODUCTION_SUB_STATE.RD_CHECKING]: '研发排查',
                [researchConstants.PRODUCTION_SUB_STATE.COMPLETED]: '完成'
            };
            return subStateMap[activeSubState] || '未知';
        }
        
        return '未知';
    }

    /**
     * 获取完整状态描述
     * @param {Object} project 项目数据
     * @returns {string} 完整状态描述
     */
    static getFullStatusDesc(project) {
        if (!project) return '未知状态';

        const level1 = project.level1;
        const level2 = project.level2;
        const level3 = project.level3;
        const level4 = project.level4;

        // 获取主状态名称
        let statusName = this.getStatusName(level1);

        // 如果是活动状态，添加活动状态名称
        if (level1 === researchConstants.PROJECT_STATUS.ACTIVE && level3) {
            const activeStateName = this.getActiveStateName(level3);
            statusName = `${statusName} - ${activeStateName}`;
            
            // 如果有活动子状态，添加活动子状态名称
            if (level4) {
                const activeSubStateName = this.getActiveSubStateName(level3, level4);
                if (activeSubStateName) {
                    statusName = `${statusName} > ${activeSubStateName}`;
                }
            }
        } 
        // 如果有子状态，添加子状态名称
        else if (level2) {
            const subStatusName = this.getSubStatusName(level1, level2);
            if (subStatusName) {
                statusName = `${statusName} > ${subStatusName}`;
            }
        }

        return statusName;
    }

    /**
     * 获取分类名称
     * @param {number} projectType 分类码
     * @returns {string} 分类名称
     */
    static getProjectTypeName(projectType) {
        switch (projectType) {
            case researchConstants.PROJECT_CATE.HARDWARE:
                return '硬件项目';
            case researchConstants.PROJECT_CATE.SOFTWARE:
                return '软件项目';
            case researchConstants.PROJECT_CATE.INTEGRATED:
                return '综合项目';
            case researchConstants.PROJECT_CATE.RESEARCH:
                return '研究项目';
            case researchConstants.PROJECT_CATE.OTHER:
                return '其他项目';
            default:
                return '未知分类';
        }
    }

    /**
     * 获取优先级名称
     * @param {number} priorityCode 优先级编码
     * @returns {string} 优先级名称
     */
    static getPriorityName(priorityCode) {
        switch (priorityCode) {
            case researchConstants.PROJECT_PRIORITY.LOW:
                return '低';
            case researchConstants.PROJECT_PRIORITY.MEDIUM:
                return '中';
            case researchConstants.PROJECT_PRIORITY.HIGH:
                return '高';
            default:
                return '未知优先级';
        }
    }

    /**
     * 计算项目进度
     * @param {Object} projectData 项目数据
     * @returns {number} 项目进度（0-100）
     */
    static calculateProgress(projectData) {
        if (!projectData) return 0;
        
        // 直接返回已保存的进度
        if (projectData.progress) {
            return parseInt(projectData.progress);
        }
        
        // 根据状态计算进度
        const level1 = projectData.level1;
        const level2 = projectData.level2;
        const level3 = projectData.level3;
        const level4 = projectData.level4;
        
        // 已完成状态
        if (level1 === researchConstants.PROJECT_STATUS.COMPLETED) return 100;
        
        // 项目审核状态
        if (level1 === researchConstants.PROJECT_STATUS.PROJECT_REVIEW) {
            if (level2 === researchConstants.PROJECT_REVIEW_SUB_STATUS.DIRECTOR) return 5;
            if (level2 === researchConstants.PROJECT_REVIEW_SUB_STATUS.RD_MANAGER) return 10;
            if (level2 === researchConstants.PROJECT_REVIEW_SUB_STATUS.ASSIGNMENT) return 15;
            return 5;
        }
        
        // 休眠状态
        if (level1 === researchConstants.PROJECT_STATUS.DORMANT) return 20;
        
        // 活动状态
        if (level1 === researchConstants.PROJECT_STATUS.ACTIVE) {
            if (!level3) return 20;
            
            // 硬件确认阶段
            if (level3 === researchConstants.ACTIVE_STATE.HARDWARE_CONFIRM) return 25;
            
            // 硬件阶段
            if (level3 === researchConstants.ACTIVE_STATE.HARDWARE) {
                if (level4 === researchConstants.HARDWARE_SUB_STATE.DESIGN) return 30;
                if (level4 === researchConstants.HARDWARE_SUB_STATE.REVIEW) return 32;
                if (level4 === researchConstants.HARDWARE_SUB_STATE.SAMPLE) return 35;
                if (level4 === researchConstants.HARDWARE_SUB_STATE.PRODUCTION_SAMPLE) return 38;
                if (level4 === researchConstants.HARDWARE_SUB_STATE.IN_STOCK) return 40;
                return 35;
            }
            
            // 调试阶段
            if (level3 === researchConstants.ACTIVE_STATE.DEBUG) return 45;
            
            // 资料同步阶段
            if (level3 === researchConstants.ACTIVE_STATE.DATA_SYNC) return 50;
            
            // 测试阶段
            if (level3 === researchConstants.ACTIVE_STATE.TESTING) {
                if (level4 === researchConstants.TESTING_SUB_STATE.TESTING) return 55;
                if (level4 === researchConstants.TESTING_SUB_STATE.NO_TEST_REVIEW) return 58;
                return 55;
            }
            
            // 客户确认阶段
            if (level3 === researchConstants.ACTIVE_STATE.CUSTOMER_CONFIRM) {
                if (level4 === researchConstants.CUSTOMER_CONFIRM_SUB_STATE.DELIVERY) return 60;
                if (level4 === researchConstants.CUSTOMER_CONFIRM_SUB_STATE.TESTING) return 65;
                if (level4 === researchConstants.CUSTOMER_CONFIRM_SUB_STATE.NO_SAMPLE_REVIEW) return 68;
                if (level4 === researchConstants.CUSTOMER_CONFIRM_SUB_STATE.SAMPLE_IN_STOCK) return 70;
                if (level4 === researchConstants.CUSTOMER_CONFIRM_SUB_STATE.ARCHIVE) return 72;
                return 65;
            }
            
            // 订单准备阶段
            if (level3 === researchConstants.ACTIVE_STATE.ORDER_PREPARATION) {
                if (level4 === researchConstants.ORDER_PREPARATION_SUB_STATE.WAITING) return 75;
                if (level4 === researchConstants.ORDER_PREPARATION_SUB_STATE.REVIEW) return 76;
                if (level4 === researchConstants.ORDER_PREPARATION_SUB_STATE.DOC_CONTROL) return 78;
                if (level4 === researchConstants.ORDER_PREPARATION_SUB_STATE.SUPPLEMENT) return 80;
                if (level4 === researchConstants.ORDER_PREPARATION_SUB_STATE.PRE_MEETING) return 82;
                if (level4 === researchConstants.ORDER_PREPARATION_SUB_STATE.PREPARATION) return 84;
                return 80;
            }
            
            // 生产阶段
            if (level3 === researchConstants.ACTIVE_STATE.PRODUCTION) {
                if (level4 === researchConstants.PRODUCTION_SUB_STATE.PRODUCING) return 85;
                if (level4 === researchConstants.PRODUCTION_SUB_STATE.PE_HANDLING) return 88;
                if (level4 === researchConstants.PRODUCTION_SUB_STATE.RD_CHECKING) return 90;
                if (level4 === researchConstants.PRODUCTION_SUB_STATE.COMPLETED) return 95;
                return 85;
            }
            
            // 待反馈阶段
            if (level3 === researchConstants.ACTIVE_STATE.PRODUCT_FEEDBACK) return 98;
            
            return 50;
        }
        
        return 0;
    }
}

module.exports = ResearchProjectModel; 